package com.jpragma.logviewer.actions {
	import com.jpragma.logviewer.events.AppSettingsEvent;
	import com.jpragma.logviewer.model.AppSettings;
	
	import flash.net.SharedObject;
	import flash.net.registerClassAlias;
	
	public class AppSettingsPersistanceAction {
		private static const SHARED_OBJECT_NAME:String = "settings";
		public function AppSettingsPersistanceAction() {
			registerClassAlias("AppSettingsAlias", AppSettings);
		}
		
		public function load():AppSettings {
			var shObj:SharedObject = SharedObject.getLocal(SHARED_OBJECT_NAME);
			var settings:AppSettings = shObj.data.appSettings;
			if (settings == null) {
				settings = new AppSettings();
			}
			return settings;
		}
		
		public function save(settings:AppSettings):void {
			var shObj:SharedObject = SharedObject.getLocal(SHARED_OBJECT_NAME);
			shObj.data.appSettings = settings;
			shObj.flush();
		}
		
		public function onSaveSettings(event:AppSettingsEvent):void {
			save(event.settings);			
		}
	}
}